package ishaportalunittests.apis.insight;

import static org.junit.Assert.assertTrue;

import java.util.List;

import gwtappcontainer.server.apis.insight.InsightAPI;
import gwtappcontainer.shared.apis.APIResponse;
import gwtappcontainer.shared.apis.APIResponse.Status;
import gwtappcontainer.shared.apis.insight.ProgramTypeProp;
import ishaportalunittests.helpers.APITestHelper;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class InsightAPIProgramTypeTest {
private final APITestHelper helper = new APITestHelper();
	
	@Before
	public void setUp() {
		helper.setUp();
	}
	
	@After 
	public void tearDown() {
		helper.tearDown();
	}
	
	@Test
	public void onlyPortalAdminCanAddProgramType() {
		//try without login
		InsightAPI api = new InsightAPI();
		APIResponse resp = api.addProgramType("test", null);
		assertTrue(resp.statusCode == Status.ERROR_LOGIN_REQUIRED);
				
		//try as developer
		resp = api.addProgramType("test", helper.loginAsDeveloper());
		assertTrue(resp.statusCode == Status.ERROR_INSUFFICIENT_PERMISSION);
		
		//portal admin
		resp = api.addProgramType("test", helper.loginAsPortalAdmin());
		System.out.println("statusCode = " + resp.statusCode);
		assertTrue(resp.statusCode == Status.SUCCESS);
		
		resp = api.listProgramTypes();
		assertTrue(resp.statusCode == Status.SUCCESS);
		
		@SuppressWarnings("unchecked")
		List<ProgramTypeProp> all = (List<ProgramTypeProp>) resp.object;
		
		assertTrue(all.size() == 1);
		assertTrue(all.get(0).name.equals("test")); //should be in upper case					
	}
	
	@Test
	public void listWorksWhenNoProgramTypePresent() {
		
		InsightAPI api = new InsightAPI();
		APIResponse resp = api.listProgramTypes();
		assertTrue(resp.statusCode == Status.SUCCESS);
		
		@SuppressWarnings("unchecked")
		List<ProgramTypeProp> all = (List<ProgramTypeProp>) resp.object;
		assertTrue(all.size() == 0);
	}
	
	@Test
	public void onlyPortalAdminUpdateProgramType() {
		InsightAPI api = new InsightAPI();
		APIResponse resp = api.addProgramType("TEST", helper.loginAsPortalAdmin());
		
		//update without login
		resp = api.updateProgramType("TEST 2", "test", null);
		assertTrue(resp.statusCode == Status.ERROR_LOGIN_REQUIRED);
		
		//update as portal user
		resp = api.updateProgramType("TEST 2", "test", helper.loginAsPortalUser());
		assertTrue(resp.statusCode == Status.ERROR_INSUFFICIENT_PERMISSION);
		
		//update as portal admin
		resp = api.updateProgramType("TEST 2", "test", helper.loginAsPortalAdmin());
		assertTrue(resp.statusCode == Status.SUCCESS);
				
		resp = api.listProgramTypes();
		@SuppressWarnings("unchecked")
		List<ProgramTypeProp> all = (List<ProgramTypeProp>) resp.object;
		assertTrue(all.get(0).name.equals("TEST 2"));
	}
	
	@Test
	public void cannotUpdateNonExistingProgramType() {
		InsightAPI api = new InsightAPI();
		APIResponse resp = api.updateProgramType("test 2", "test", helper.loginAsPortalAdmin());
		
		assertTrue(resp.statusCode == Status.ERROR_RESOURCE_DOES_NOT_EXIST);
	}
	
	@Test
	public void cannotAddDuplicateProgramType() {
		InsightAPI api = new InsightAPI();
		APIResponse resp = api.addProgramType("test", helper.loginAsPortalAdmin());
		
		assertTrue(resp.statusCode == Status.SUCCESS);
		
		//add it again
		resp = api.addProgramType("test", helper.loginAsPortalAdmin());
		assertTrue(resp.statusCode == Status.ERROR_RESOURCE_ALREADY_EXISTS);
		
		//change case and add it again
		resp = api.addProgramType("teSt", helper.loginAsPortalAdmin());
		assertTrue(resp.statusCode == Status.ERROR_RESOURCE_ALREADY_EXISTS);
	}
}
